/*设计一个链表类，能存储整形的数据，要求有数据的增、删、改、查相关方法。
（必须有构造和析构方法，注意内存的管理）*/

#include <iostream>
using namespace std;

struct myNode
{
    int numb;
    myNode *pnext;
};

class data
{
private:
    myNode *head;
    int len;
public:
    //构造
    data()
    {
        this->head = new myNode;
        cout<<"please input 1st number:"<<endl;
        cin>>this->head->numb;
        this->len = 1;
    }
    //析构
    ~data()
    {
        myNode *node = this->head;
        myNode *next = node->pnext;
        while(1)
        {
            delete(node);
            node = next;
            if(next == 0)
            {
                break;
            }
            next = next->pnext;
        }
    }
    //增
    void addNode()
    {
        myNode *node = new myNode;
        cout<<"please input one number:"<<endl;
        cin>>node->numb;
        node->pnext = this->head;
        this->head = node;
        this->len++;

    }
    //删
    void delNode()
    {
        int x;
        cout<<"please input one number you wanto del:"<<endl;
        cin>>x;
        myNode *pre = this->head;
        myNode *ure = pre->pnext;
        int flag = 0;
        while(ure != 0)
        {
            if(ure->numb == x)
            {
                if(pre == this->head)
                {
                    this->head = ure->pnext;
                    delete(ure);
                    this->len--;
                    ure = pre->pnext;
                    flag = 1;
                }
                else
                {
                    pre->pnext = ure->pnext;
                    delete(ure);
                    this->len--;
                    ure = pre->pnext;
                    flag = 1;
                }
            }
            if(ure->pnext == 0)
            {
                break;
            }
            ure = ure->pnext;
            pre = pre->pnext;
        }
        if(flag == 0)
        {
            cout<<"have no found!"<<endl;
        }
    }
    //改
    void changeNode()
    {
        int x;
        cout<<"please input one number you wanto change:"<<endl;
        cin>>x;
        myNode *node = this->head;
        int flag = 0;
        while(node != 0)
        {
            if(node->numb == x)
            {
                int y;
                cout<<"please input change to:"<<endl;
                cin>>y;
                node->numb = y;
                flag = 1;
            }
            node = node->pnext;
        }
        if(flag == 0)
        {
            cout<<"have no found!"<<endl;
        }
    }
    //查
    void searchNode()
    {
        int x;
        cout<<"please input one number you wanto search:"<<endl;
        cin>>x;
        myNode *node = this->head;
        int flag = 0;
        while(node != 0)
        {
            if(node->numb == x)
            {
                cout<<"the number is:"<<node->numb<<endl;
                flag == 1;
            }
            node = node->pnext;
        }
        if(flag == 0)
        {
            cout<<"have no found!"<<endl;
        }
    }
};

int main(int argc,char*argv[])
{
    data nubs;
    nubs.addNode();
    nubs.addNode();
    nubs.addNode();
    nubs.addNode();
    nubs.addNode();
    nubs.delNode();
    nubs.changeNode();
    nubs.searchNode();
    return 0;
}